package com.sandwich.api.controller.v1;
import com.alibaba.fastjson.JSON;
import com.sandwich.admin.domain.ContentSummaryEntity;
import com.sandwich.admin.service.ContentSummaryService;
import com.sandwich.api.apibean.v1.ContentRequest;
import com.sandwich.api.apibean.v1.ContentSummary;
import com.sandwich.common.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description 房源信息
 * @date 2021-07-28
 */
@RestController("ApiContentSummaryControllerV1")
@RequestMapping("/api/v1/content")
public class ContentSummaryController {

    @Autowired
    private ContentSummaryService contentSummaryService;

    @PostMapping("/query/categoryContent")
    public ResponseEntity<?> findCategoryContent(HttpServletRequest request,
                                                 @RequestHeader HttpHeaders headers,
                                                 @RequestBody ContentRequest contentRequest) {
        Page<ContentSummaryEntity> categorySummaryEntities = contentSummaryService.findCategorySummaryEntityPage(contentRequest);
        int totalPage = categorySummaryEntities.getTotalPages();
        List<ContentSummary> categoryResponses = categorySummaryEntities.getContent().stream()
                .map(e -> {
                    ContentSummary contentSummary = JSON.parseObject(e.getContentJson(), ContentSummary.class);
                    contentSummary.setContentSummaryPk(e.getPk());
                    return contentSummary;
                }).collect(Collectors.toList());
        PageResponse pageResponse = new PageResponse();
        pageResponse.setTotalPage(totalPage);
        pageResponse.setContents(categoryResponses);
        return new ResponseEntity(pageResponse, HttpStatus.OK);
    }
}